package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.entity.*;
import com.atguigu.eduservice.entity.subject.OneSubject;
import com.atguigu.eduservice.entity.subject.TwoSubject;
import com.atguigu.eduservice.entity.vo.CoursePublishVo;
import com.atguigu.eduservice.entity.vo.CourseVo;
import com.atguigu.eduservice.mapper.EduCourseMapper;
import com.atguigu.eduservice.service.*;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.util.BeanUtil;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author kaixuan
 * @since 2020-05-30
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    @Autowired
    private EduCourseDescriptionService courseDescriptionService;

    @Autowired
    private EduSubjectService subjectService;

    @Autowired
    private EduTeacherService teacherService;

    @Autowired
    private EduVideoService videoService;

    @Autowired
    private EduChapterService chapterService;

    /**
     * 添加课程基本信息
     * @param courseVo
     */
    @Override
    @Transactional
    public String saveCourseInfo(CourseVo courseVo) {
        String[] arrIds = courseVo.getIds();
        courseVo.setSubjectParentId(courseVo.getIds()[0]);
        courseVo.setSubjectId(courseVo.getIds()[1]);
        //向课程表添加基本信息
        //CourseVo转换为EduCourse对象
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseVo,eduCourse);
        baseMapper.insert(eduCourse);
        //想课程简介表添加简介
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setDescription(courseVo.getDescription());
        eduCourseDescription.setId(eduCourse.getId());
        courseDescriptionService.save(eduCourseDescription);
        return eduCourse.getId();

    }

    /**
     * 根据课程id查询课程基本数据
     * @param courseId
     * @return
     */
    @Override
    public Map<String,Object> getCourseVo(String courseId) {
        Map<String,Object> map = new HashMap<>();
        CourseVo courseVo = new CourseVo();

        EduCourse course = baseMapper.selectById(courseId);
        BeanUtils.copyProperties(course,courseVo);

        EduCourseDescription courseDescription = courseDescriptionService.getById(courseId);
        courseVo.setDescription(courseDescription.getDescription());

        //传入teacher数据
        List<EduTeacher> teacherList = teacherService.list(null);

        //传入所有的nodeList
        List<OneSubject> subjectList = subjectService.nestedList();
        map.put("courseVo",courseVo);
        map.put("items",subjectList);
        map.put("teacherList",teacherList);
        return map;
    }

    /**
     * 修改课程基本信息
     * @param courseVo
     * @return
     */
    @Override
    @Transactional
    public String updateCourseVo(CourseVo courseVo) {
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseVo,eduCourse);
        baseMapper.updateById(eduCourse);

        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        BeanUtils.copyProperties(courseVo,eduCourseDescription);
        courseDescriptionService.updateById(eduCourseDescription);

        return eduCourse.getId();
    }

    /**
     * 根据课程id获取关联数据
     * @param courseId
     * @return
     */
    public Map<String,Object> getCoursePublishVo(String courseId){
        Map<String,Object> map = new HashMap<>();
        //传入所有的nodeList
        List<OneSubject> subjectList = subjectService.nestedList();
        CoursePublishVo coursePublishVo = baseMapper.getCoursePublishVo(courseId);
        map.put("items",subjectList);
        map.put("coursePublishVo",coursePublishVo);
        return map;
    }

    /**
     * 删除课程 章节 小节 视频
     * @param courseId
     * @return
     */
    @Override
    @Transactional
    public boolean removeAllData(String courseId) {
        // 从最底层删起  小节-->章节-->描述-->课程
        videoService.removeVideo(courseId);
        chapterService.removeChapterByCourseId(courseId);
        courseDescriptionService.removeById(courseId);
        int row = baseMapper.deleteById(courseId);
        if (row > 0){
            return true;
        }else{
            return false;
        }
    }
}
